package com.xyc.manager.permission.service.impl;

import com.xyc.manager.permission.bean.ChildTreeBean;
import com.xyc.manager.permission.bean.ParentTreeBean;
import com.xyc.manager.permission.bean.SysRole;
import com.xyc.manager.permission.bean.TreeViewState;
import com.xyc.manager.permission.mapper.PermissionMapper;
import com.xyc.manager.permission.service.PermissionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;
    public List<ParentTreeBean> queryParentTreeBeans(String account) {
        return permissionMapper.queryParentTreeBeans(account);
    }

    @Override
    public List<ParentTreeBean> queryRolePermissionByRoleCode(String roleCode) {
        //查询所有权限
        List<ParentTreeBean> parentTreeBeans = permissionMapper.queryAllPermission();

        //查询角色拥有的权限
        List<ParentTreeBean> hasTreeBeans = permissionMapper.queryRolePermissionByRoleCode(roleCode);


        for (ParentTreeBean parentTreeBean : parentTreeBeans){//遍历得到所有的一级目录

            String parentTreeBeanText = parentTreeBean.getText();

            for (ParentTreeBean hasTreeBean : hasTreeBeans){
                String hasTreeBeanText = hasTreeBean.getText();

                if (parentTreeBeanText.equals(hasTreeBeanText)){//判断一级权限是否拥有
                    parentTreeBean.setFid(hasTreeBean.getFid());

                    TreeViewState parentState = new TreeViewState();//修改选中状态
                    parentState.setChecked(true);
                    parentTreeBean.setState(parentState);

                    //1、如果有子权限、一定有父权限
                    //2、如果有父权限、不一定有子权限

                    //判断子权限
                    List<ChildTreeBean> allNodeList =parentTreeBean.getNodes();
                    for (ChildTreeBean allNode : allNodeList){//遍历二级目录
                        String allNodeText = allNode.getText();

                        List<ChildTreeBean> hasNodeList = hasTreeBean.getNodes();
                        for (ChildTreeBean hasNode : hasNodeList){//判断二级权限是否拥有
                            String hasNodeText = hasNode.getText();
                            if (allNodeText.equals(hasNodeText)){
                                allNode.setFid(hasNode.getFid());

                                TreeViewState childState = new TreeViewState();
                                childState.setChecked(true);
                                allNode.setState(childState);


                            }
                        }
                    }
                }
            }
        }

        return parentTreeBeans;
    }
    //返回查找到的fid
    @Override
    public Integer queryFidByModuleCodeAndRoleId(String roleId,String moduleCode) {
        return permissionMapper.queryFidByModuleCodeAndRoleId(roleId,moduleCode);
    }

    @Override
    public List<SysRole> queryRoleList() {
        return permissionMapper.queryRoleList();
    }

    @Override
    public void cancelPermission(Integer fid, String roleId) throws Exception {
        //如果这个父权限还有子权限，连同子权限一起删除。
        //所有的子权限
        List<String> childPermissionCodeList = permissionMapper.queryChildPermissionCode(fid);
        if (childPermissionCodeList != null && !childPermissionCodeList.isEmpty()){
            for (String moduleId:childPermissionCodeList){
                permissionMapper.cancleChildPermissionByParent(moduleId,roleId);
            }
        }
        //删除父权限
        permissionMapper.canclePermission(fid);
    }

    /**
     * 有些问题
     * @param roleId
     * @param moduleCode
     */
    @Override
    public void addPermission(String roleId, String moduleCode){
        //如果这个子权限有父权限，连同父权限一起添加。
        //根据子权限查询父权限
        String parentPermissionCode = permissionMapper.queryParentPermissionCode(moduleCode);
        List<String> childPermissionCodeList = permissionMapper.queryChildPermissionCodeList(moduleCode);

        List<Integer> fidList = permissionMapper.queryFidListByModuleCodeAndRoleId(roleId, parentPermissionCode);
    //    List<Integer> fidAllList = permissionMapper.queryFidListByModuleCodeAndRoleId(roleId,moduleCode);
        if (parentPermissionCode != null && !parentPermissionCode.isEmpty() && parentPermissionCode.length()>=6){
            if (fidList.size() > 0){

                permissionMapper.addPermission(roleId, moduleCode);//添加子权限
            }else{
                permissionMapper.addPermission(roleId,parentPermissionCode);//添加父权限
                permissionMapper.addPermission(roleId, moduleCode);//添加子权限
            }

        }else {

            Integer fid = permissionMapper.queryFidByModuleCodeAndRoleId(roleId, moduleCode);
            try {
                cancelPermission(fid,roleId);
            } catch (Exception e) {
                e.printStackTrace();
            }
            permissionMapper.addPermission(roleId,moduleCode);//添加父权限
            for (String childPermissionCode : childPermissionCodeList){
                permissionMapper.addPermission(roleId,childPermissionCode);//当选中父类就选中所有子类
            }
        }
        //刷新页面中的数据


       // permissionMapper.queryAllPermission();
    }
}
